package mas.experiment_2_wordbook.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;

import mas.experiment_2_wordbook.MainActivity;
import mas.experiment_2_wordbook.R;

public class Login extends AppCompatActivity {
    private EditText name;
    private EditText password;
    private CheckBox savePassword;
    private CheckBox isPasswordVisable;
    private Button login;
    private SharedPreferences mySharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mySharedPreferences = getSharedPreferences("test", Activity.MODE_PRIVATE);
        name = findViewById(R.id.name);
        password = findViewById(R.id.password);
        isPasswordVisable = findViewById(R.id.show_password);
        savePassword = findViewById(R.id.save_password);
        login = findViewById(R.id.login);
        boolean isSavePassWord = mySharedPreferences.getBoolean("isSavePassWord",false);
        name.setText(mySharedPreferences.getString("author",""));
        if(isSavePassWord){
            password.setText(mySharedPreferences.getString("password",""));
            savePassword.setChecked(true);
        }
        isPasswordVisable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    //如果选中，显示密码
                    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //否则隐藏密码
                    password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }

            }
        });
    }
    //在xml中使用android：onClick相当于代替了监听器
    public void onClick(View view){

        //实例化SharedPreferences.Editor对象
        editor = mySharedPreferences.edit();
        //用putString的方法保存数据
        String username = mySharedPreferences.getString("author",null);
        String userpwd = mySharedPreferences.getString("password","");
        if(username==null||(name.getText().toString().equals(username)&&password.getText().toString().equals(userpwd))){
            if(savePassword.isChecked()){
                editor.putBoolean("isSavePassWord",true);
                editor.putString("password",password.getText().toString());
                editor.putString("author", name.getText().toString());
            }else{
                editor.clear();
            }
            editor.commit();
            Intent intent = new Intent(Login.this, MainActivity.class);
            startActivity(intent);
        }else{
            Toast.makeText(Login.this, "密码不正确", Toast.LENGTH_SHORT).show();
        }
    }
}
